<?php
namespace Home\Controller;		//这个目录下的文件
use Think\Controller;			//运用这个地方的基类
class LoginController extends Controller {
	

    public function index(){
    	$sql = "select * from vercode order by rand() limit 1";//任意排序取第一条
    	$res = M()->query($sql);	//实例化操作数据库的类用其中的query函数
    	$_SESSION['answer']=$res[0]['answer'];	//数据库的答案放在session中
    	$this->assign('res',$res);			
    	//将数组$res放在全局变量res那里，这样html就可以使用$res了
        $this->display();		//引入登录html的函数

    }

  
   

   public function check(){
   	$vercode =I('post.vercode','');
   	if ($vercode!=$_SESSION['answer']) {
   		$arr=array(
   			'error'=>1,
   			'info'=>'答案错误'
   			);
   		echo json_encode($arr);
   		exit;
   	}else{
   		$email =I('post.email','');
   		$password =md5(I('post.password',''));
   		$sql = "select * from user where email='{$email}' and password='{$password}'";
   		$res=M()->query($sql);

   		if ($res) {
   			$_SESSION['nikename'] = $res[0]['nikename'];
    		$_SESSION['uid'] = $res[0]['id'];
    		$_SESSION['face'] = $res[0]['face'];
        $_SESSION['sex'] = $res[0]['sex'];
   			$arr=array(
   				'error'=>0,
   				'info'=>'登录成功'
   				);
   			echo json_encode($arr);
   		}else{
   			$arr=array(
   				'error'=>1,
   				'info'=>'密码或者邮箱错误'
   				);
   			echo json_encode($arr);
   		}
   	}
   }
    public function loginout()
    {
    	session_unset();
    	session_destroy();
    	$this->success('退出成功',U('home/index/index'));
    }

    public function qqreturn()  //接收
    {
        //$_REQUEST接收所有数据

      $openid=getOpenid();
      $sql="select * from user where openid='{$openid}'";
      $res=M()->query($sql);

      if ($res) {
        $_SESSION['nikename']=$res[0]['nikename'];
        $_SESSION['uid']=$res[0]['id'];
        $_SESSION['face']=$res[0]['face'];
        $this->success('登录成功',U('home/user/index',3));
      }else{
        $info=getInfofn();
        $info['openid']=$openid;
        $info['face']=$info['figureurl_qq_1'];
        $info['sex']=$info['gender'];
        $info['nikename']=$info['nickname'];
        $info['create_time']=time();
        $sql_add=M('user')->add( $info); 
        //增加信息成功后结果返回的是id值
        if ($sql_add) {
          $_SESSION['nikename']=$info['nikename'];
          $_SESSION['uid']=$sql_add;
          $_SESSION['face']=$info['face'];
          $this->success('登录成功',U('home/user/index',3));

          //初始化
          $ch = curl_init();
          //设置选项，包括URL
          curl_setopt($ch, CURLOPT_URL, $_SESSION['face']);
          curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
          curl_setopt($ch, CURLOPT_HEADER, 0);
          //执行并获取HTML文档内容
          $output = curl_exec($ch);
          //释放curl句柄
          curl_close($ch);
          //打印获得的数据
          file_put_contents('./uploads/qqface/'.$_SESSION['uid'].'.jpg', $output);




        }else{
          $this->error('登录失败',U('home/login/index',3));
        }
      }












    }

    public function qqsend()    //发送,同时要更改html页面的相对应图标网址
    {
      qqsend();    
      //是公用common文件夹里面自己建的方法，运行后就连接qq服务器,同时qq会返回一些数据作为令牌(返回的东西是在返回域里面的)，tp就可以根据这些令牌获取qq信息
    }








}
